package com.example.okhttpstudy;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity {

    private String url = "https://www.wanandroid.com/banner/json";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
//        okHttpUse();
        for (int i = 0; i < 10; ++i) {
            sendRequest();
        }
    }

    private void sendRequest() {
        TestHttp.sendJsonRequest(url, null, Bean.class, new IJsonDataTransformListener<Bean>() {
            @Override
            public void onSuccess(Bean o) {
                Log.e("=======>", o.toString());
            }
        });
    }

    private void okHttpUse() {
        OkHttpClient okHttpClient = new OkHttpClient.Builder().build();
        final Request request = new Request.Builder().url("https://www.wanandroid.com/banner/json").build();
        Call call = okHttpClient.newCall(request);
        // 异步处理方式
        // 第一条主线：请求发送到哪去了；
        // 第二条主线：请求是被谁处理的；
        // 第三条主线：请求是怎么被维护的；
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                System.out.println("请求失败");
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String content = response.body().string();
                System.out.println("请求成功" + content);
            }
        });
    }
}